var express = require('express');
var conn = require('../public/javascripts/connect');
var router = express.Router();

/* GET home page. */
router.get('/', async function (req, res, next) {
    let Year, Month, tableArr = [], user = {};
    let daparentName=decodeURIComponent(req.query.tableName.split(",")[0]);
    // 物流装备事业部_2018_9
    user.dpN=daparentName.split("_")[0];
    user.Year=daparentName.split("_")[1];
    user.Month=Number(daparentName.split("_")[2]);
    user.Gread=decodeURIComponent(req.query.tableName.split(",")[1]);
    if(user.Gread==1){
        user.TitleBar=`金山物流${user.Year}年${user.Month}月份${user.Gread}级巴经营会计损益表`;
    }else if(user.Gread==2){
        user.TitleBar=`${user.dpN}${user.Year}年${user.Month}月份${user.Gread}级巴经营会计损益表(${user.dpN})`;
    }else if(user.Gread==3){
        user.TitleBar=`${user.dpN}${user.Year}年${user.Month}月份${user.Gread}级巴经营会计损益表`;
    }

    //查询语句
    let sql = `SELECT * FROM ${daparentName} ORDER BY 行号`;
    let sqlZiduan = `SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE table_name = '${daparentName}'`;
    await new Promise(resP=>{
        conn.connection2.query(sqlZiduan, function (err, result, fields) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            }
            user.data_field = result;
            resP(true)
        });
    })

    console.log(sql);
    conn.connection2.query(sql, function (err, result, fields) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }
        for (let i in result) {
            for (let j in result[i]){
                if(j==="id"||j==="行号"||j==="科目"||j==="年份"||j==="月份"){
                    continue;
                }else if(j==="去年同期比率"||j==="去年同期累计比率"||j==="年度计划金额比率"||j==="当月累计比率"||j==="当月累计完成率"||j==="累比增减率"||j==="上月比率"||j==="当月计划比率"||j==="当月比率值"||j==="当月实际完成率"||j==="环比增降幅"||j==="同比增降幅"||j==="计划增降幅"||j==="下月比率值"){
                    result[i][j]=(Number(result[i][j])*100).toFixed(2)+"%";
                    continue;
                }
                if(typeof result[i][j]=== 'number'){
                    result[i][j]=result[i][j].toFixed(2)
                }
                if(j.indexOf("修改日期")!=-1) {
                    if(null == result[i][j] || "undefined" == typeof(result[i][j]) || "" == result[i][j]){
                    }else{
                        result[i][j] = result[i][j].toLocaleDateString();
                    }

                }
            }
        }
        user.data_result = result
        res.render('viewtable', user);
    });
});
router.post("/",async function(req,res,next){
    let {lineNumber,field,valnum,tableName}=req.body;
    let sql = `UPDATE ${tableName} SET ${field}=${valnum} WHERE 行号=${lineNumber}`;
    conn.connection2.query(sql,function (err,result,filed) {
        if(err){
            return '[UPDATE ERROR]::'+err;
        }
        console.log("result",result)
        res.json({Modification:true})
    })
    console.log(lineNumber);
    console.log(field);
    console.log(valnum);
    console.log(tableName);

})

module.exports = router;